﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace LabMonitorRemote
{
    class BinaryFileWriter
    {
        string downloadFileName;
        string loggerName;
        string loggerIPAddress;
        String _DownloadFolder = Properties.Settings.Default.DownloadFolder;
        FileStream downloadFileStream;
        List<byte[]> downloadByteStream = new List<byte[]>();
        
        //public BinaryFileWriter(LoggerDownload callingDownload)
        public BinaryFileWriter(IPEndPoint LoggerIP)
        {
            //loggerName = callingDownload.LoggerName;
            //loggerIPAddress = callingDownload.NetworkEndpoint.Address.ToString();
            loggerName = "";
            loggerIPAddress = LoggerIP.Address.ToString();

        }

        public BinaryFileWriter(String LoggerName, IPEndPoint LoggerIP)
        {
            //loggerName = callingDownload.LoggerName;
            //loggerIPAddress = callingDownload.NetworkEndpoint.Address.ToString();
            loggerName = LoggerName;
            loggerIPAddress = LoggerIP.Address.ToString();

        }

        public BinaryFileWriter(DewkLogger dewk)
        {
            
        }

        public void WriteDataToFile()
        {
            WriteBinaryDataToFile();
        }

        public void AppendBytes(byte[] buffer, Int32 offset, Int32 length)
        {
            try
            {

                byte[] block = new byte[length - offset];

                for (int i = 0; i < length - offset; i++)
                {
                    block[i] = buffer[i + offset];
                }

                downloadByteStream.Add(block);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                //MessageBox.Show(e.Message + "\n\nLength: " + length + ";  Offset: " + offset);
            }

        }

        //CREATE FILENAME FOR BINARY DATA STORAGE
        private Boolean CreateFileName()
        {
            if (!string.IsNullOrEmpty(loggerName) && !string.IsNullOrEmpty(loggerIPAddress))
            {
                string fileName = _DownloadFolder + "\\";
                string timestamp = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second;
                fileName = fileName + loggerName + " - " + loggerIPAddress + " - " + timestamp + ".rec";
                downloadFileName = fileName;
                return true;
            }
            else
            {
                return false;   
            }
        }//END CreateFileName
    
        //WRITE BINARY DATA TO FILE
        private void WriteBinaryDataToFile()
        {
            if (CreateFileName())
            {
                try
                {
                    Int32 offset = 0;
                    downloadFileStream = File.OpenWrite(downloadFileName);
                    foreach (byte[] block in downloadByteStream)
                    {
                        downloadFileStream.Write(block, 0, block.Length);
                        offset += block.Length;
                    }
                    downloadFileStream.Close();
                }
                catch (Exception e)
                {
                    // print error
                }
                finally
                {
                    if (downloadFileStream != null)
                        downloadFileStream.Close();

                }
            }
        }
    
    }
}
